﻿#region File Description
//-----------------------------------------------------------------------------
// TaskSet
//
// Copyright © 2013 Wave Coorporation. All rights reserved.
// Use is subject to license terms.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using WaveEngine.Framework;
#endregion

namespace WaveEngine.Extensions.ITasks
{

    class WaitCountDeferedTaskSet : WaitCountTaskSetBase<Func<ITask>>
    {
        /// <summary>
        /// The created tasks
        /// </summary>
        ITask[] createdTasks = null;

        /// <summary>
        /// Initializes a new instance of the <see cref="WaitCountDeferedTaskSet" /> class.
        /// </summary>
        /// <param name="parent">The parent.</param>
        /// <param name="countLimit">The count limit.</param>
        /// <param name="childTasks">The child tasks.</param>
        public WaitCountDeferedTaskSet(ITask parent, string name, int countLimit, params Func<ITask>[] childTasks)
            : base(parent, name, countLimit, childTasks)
        {

        }

        /// <summary>
        /// Initializes a new instance of the <see cref="WaitCountDeferedTaskSet" /> class.
        /// </summary>
        /// <param name="scene">The scene.</param>
        /// <param name="countLimit">The count limit.</param>
        /// <param name="childTasks">The child tasks.</param>
        public WaitCountDeferedTaskSet(Scene scene, string name, int countLimit, params Func<ITask>[] childTasks)
            : base(scene, name, countLimit, childTasks)
        {
        }

        /// <summary>
        /// Notifies the cancelled.
        /// </summary>
        protected override void performCancel()
        {
            foreach (ITask t in this.createdTasks)
            {
                if (t.TaskState == WaveEngine.Framework.Services.TaskState.Running || t.TaskState == WaveEngine.Framework.Services.TaskState.Waiting)
                    t.Cancel();
            }
            base.performCancel();
        }

        /// <summary>
        /// Notifies the skip.
        /// </summary>
        /// <returns></returns>
        protected override bool performSkip()
        {
            if (this.IsSkippable)
            {
                foreach (ITask t in this.createdTasks)
                {
                    if (t.TaskState == WaveEngine.Framework.Services.TaskState.Running)
                    {
                        t.Cancel();
                    }
                }
                base.performSkip();
                return true;
            }
            else
            {
                bool skip = true;
                foreach (ITask t in this.createdTasks)
                {
                    if (t.TaskState == WaveEngine.Framework.Services.TaskState.Running)
                    {
                        skip = skip & t.TrySkip();
                    }
                }
                return skip;
            }
        }

        /// <summary>
        /// Ons the run.
        /// </summary>
        protected override void performRun()
        {
            base.performRun();

            this.createdTasks = new ITask[this.childTasks.Length];
            for (int i = 0; i < this.childTasks.Length; i++)
            {
                ITask task = childTasks[i]();
                createdTasks[i] = task;
                task.Completed += task_Completed;
                task.Cancelled += task_Cancelled;
                task.Run();
            }
        }

        /// <summary>
        /// Gets the child tasks.
        /// </summary>
        /// <value>
        /// The child tasks.
        /// </value>
        public override IEnumerable<ITask> ChildTasks
        {
            get { return this.createdTasks; }
        }
    }
}

